/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.util.Vector;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

/**
 *
 * @author LTTDNQ
 */
public class WrapText {

    private int width;
    private int lenghStr;
    private int pos, start;
    private int viTriX, viTriY;
    private int dong;
    private int align;
    private Graphics g;
    private String text;
    private Font font;

    public WrapText(Graphics g, String s, int x, int y,int w, Font f,int alignment) {
        font = f;
        text = s;
        this.g = g;
        lenghStr = text.length();
        width = w;
        viTriX = x;
        viTriY = y;
        align = alignment;
    }

    public int write() {

        //reseting
        pos = 0;
        start = 0;

        int fontHight = font.getHeight() + 1;
        String temp;
        g.setFont(font);
        while (hasMoreLines()) {
            temp = nextLine().trim();
            g.drawString(temp, viTriX, viTriY, Graphics.TOP | align);
            viTriY += fontHight;
        }
        return viTriY;
    }

    private boolean hasMoreLines() {
        return (pos < (lenghStr - 1));
    }

    private String nextLine() {
        int maxLength = text.length();
        int next = next();
        if (start >= maxLength || next > maxLength) {
            return null;
        }
        String s = text.substring(start, next);
        start = next;
        if ((text.length() - 1 > start) && ((text.charAt(start) == '\n')
                || (text.charAt(start) == ' '))) {
            pos++;
            start++;
        }
        return s;
    }

    private int next() {
        int i = getNextWord(pos);
        int lastBreak = -1;
        String line;
        line = text.substring(pos, i);
        int lineWidth = font.stringWidth(line);
        while (i < lenghStr && lineWidth <= width) {
            if (text.charAt(i) == ' ') {
                lastBreak = i;
            } else if (text.charAt(i) == '\n') {
                lastBreak = i;
                break;
            }
            if (++i < lenghStr) {
                i = getNextWord(i);
                line = text.substring(pos, i);
                lineWidth = font.stringWidth(line);
            }
        }
        if (i == lenghStr && lineWidth <= width) {
            pos = i;
        } else if (lastBreak == pos) {
            pos++;
        } else if (lastBreak < pos) {
            pos = i;
        } else {
            pos = lastBreak;
        }
        return pos;
    }

    private int getNextWord(int startIndex) {
        int space = text.indexOf(' ', startIndex);
        int newLine = text.indexOf('\n', startIndex);
        if (space == -1) {
            space = lenghStr;
        }
        if (newLine == -1) {
            newLine = lenghStr;
        }
        if (space < newLine) {
            return space;
        } else {
            return newLine;
        }
    }
}